package com.zhiwei.controller;

import com.zhiwei.pojo.User;
import com.zhiwei.service.UserService;
import com.zhiwei.tool.UserMsgUtil;
import com.zhiwei.vo.SysResult;
import com.zhiwei.vo.UserInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
    @GetMapping("/getMsgNum/{userId}")
    public SysResult getMsgNum(@PathVariable Integer userId){
        int i = userService.getMsgNum(userId);
        return SysResult.success(i);
    }
    @PutMapping("/cleanMsgNum/{userId}")
    public SysResult cleanMsgNum(@PathVariable Integer userId){
        int i = UserMsgUtil.cleanMsgNum(userId);
        return SysResult.success(i);
    }
    @GetMapping("/getlist")
    public SysResult getlist(){
        List<User> list = userService.getlist();
        return SysResult.success(list);
    }
    //用户登录
    @PostMapping("/login")
    public SysResult login(@RequestBody User user){
        UserInfo userInfo = userService.login(user);
        if (StringUtils.hasLength(userInfo.getToken())){
            return SysResult.success(userInfo);
        }
        return SysResult.fail();
    }
    //用户添加
    @PostMapping("/addUser")
    public SysResult addUser(@RequestBody User user){
        if (userService.addUser(user) != 0){
            return SysResult.success();
        }
        return SysResult.fail();
    }
    //查询用户是否存在
    @GetMapping("/find/{name}")
    public SysResult findUser(@PathVariable String name){
        Integer i = userService.findUser(name);
        if (i==1){
            return SysResult.success(1);
        }
        return SysResult.success(0);
    }
    @GetMapping("/getById/{id}")
    public SysResult getUserById(@PathVariable Integer id){
        User user = userService.getUserById(id);
        if (user != null){
            return SysResult.success(user);
        }
        return SysResult.fail();
    }
}
